package com.caldremch.calhttp.http.api;

import com.caldremch.calhttp.http.interceptor.HttpLoggingInterceptor;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;

import java.util.concurrent.TimeUnit;

import okhttp3.OkHttpClient;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * @author Caldremch
 * @date 2018-01-19 15:05
 * @describe
 **/
public class ApiFactory {
    private Retrofit mRetrofit;
    private ApiService mService;

    private ApiFactory() {

        HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor("caldremch");
        interceptor.setPrintLevel(HttpLoggingInterceptor.Level.BODY);
        OkHttpClient okHttpClient = new OkHttpClient.Builder()
                .readTimeout(30, TimeUnit.SECONDS)
                .connectTimeout(30, TimeUnit.SECONDS)
                .readTimeout(30, TimeUnit.SECONDS)
                .addInterceptor(interceptor)
                .build();
        Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd HH:mm:ss").serializeNulls().create();
        mRetrofit = new Retrofit.Builder()
                .client(okHttpClient)
                .addConverterFactory(GsonConverterFactory.create())
                .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
                .baseUrl("https://api2.zyxr.com/MobileWeb/")
                .build();
        mService = mRetrofit.create(ApiService.class);
    }


    private static class SingletonHolder {
        private static final ApiFactory INSTANCE = new ApiFactory();
    }

    public static ApiService getApiService() {
        return SingletonHolder.INSTANCE.mService;
    }

}
